<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>栈数据结构的实现</title>
</head>

<body>
    <script>
        //具体内容见文章：https://blog.csdn.net/weixin_46318413/article/details/122523233
        class Stack {
            //构造器
            constructor() {
                this.items = [];
            }
            //入栈（加到栈顶）
            in(value) {
                //一般[0]的位置时栈底
                this.items.push(value)
            }
            //出栈（遵循后入(栈顶)先出的原则）
            out() {
                return this.size() > 0 ? this.items.pop() : null;
            }
            //弹栈（取到栈顶的值,不删除）
            top() {
                return this.size() > 0 ? this.items[this.size() - 1] : null;
            }
            //计算栈的长度
            size() {
                return this.items.length
            }
        }

        //实例化对象
        let stack = new Stack();
        stack.in(0);
        stack.in(1);
        stack.in(2);
        console.log(stack.items)// [0, 1, 2]
        stack.out();
        console.log(stack.items)// [0, 1]
        console.log(stack.top())// 1
        console.log(stack.size())// 2
    </script>
</body>

</html>